package utils;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.SystemClock;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import student.andlei.com.student.R;
import ui.activity.AddNewNoteActivity;

public class XuanFuKuang {
    private WindowManager wManager;
    private boolean is_begin;
    private Context context;
    private int w=  DensityUtil.dp2pxInt(90);
    private int h=  DensityUtil.dp2pxInt(90);
    private View view;
    private  WindowManager.LayoutParams wmParams;
    private int startx;
    private int starty;
    private TextView bt_;

    public XuanFuKuang(final Context context) {
        this.context = context;
        wManager = (WindowManager) context.getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.layout_test_rela_view, null);
        bt_ = view.findViewById(R.id.tv_notebook);

        initView();
        initparams();
    }

    private void initparams() {
        wmParams = new WindowManager.LayoutParams();
        // 设置窗口的类型这里如果设置成TYPE_PHONE，那么需要权限，设置成Type_tosast不需要权限就可运行，但是可能在19版本以下的
        //版本会有问题，这里可以判断一下版本，大于19就用TYPE_TOAST
        wmParams.type = WindowManager.LayoutParams.TYPE_TOAST;

        // 设置行为选项
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        wmParams.format = PixelFormat.RGBA_8888;
        wmParams.dimAmount = 0.5f;
        wmParams.gravity= Gravity.TOP|Gravity.LEFT;
        wmParams.x=0;
        wmParams.y=0;
        // 设置悬浮窗口长宽数据
        wmParams.width = DensityUtil.dp2pxInt(90);
        wmParams.height = DensityUtil.dp2pxInt(90);
    }

    public synchronized void begin() {
        if (!is_begin) {
            is_begin = true;
            wManager.addView(view, wmParams);
        }
    }


    //关闭悬浮框
    public synchronized void release() {
        if (is_begin) {
            wManager.removeView(view);
            is_begin = false;
        }
    }

    /**
     * 返回是否开启
     */
    public boolean isBegin(){
        return is_begin;
    }

    private void setParms(int x,int y){
        wmParams.x=x;
        wmParams.y=y;
    }
    private long clickTime=0;
    private boolean isMove;
    private void  initView() {
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float x = event.getRawX();
                float y = event.getRawY();
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        startx= (int) event.getX();
                        starty= (int) event.getY();
                        //当前系统时间的毫秒值
                        clickTime = SystemClock.uptimeMillis();
                        isMove =false;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        setParms((int) x-startx, (int) y-starty);
                        wManager.updateViewLayout(view, wmParams);
                        isMove = true;
                        break;
                    case MotionEvent.ACTION_UP:
                        if (SystemClock.uptimeMillis() - clickTime <= 1000) {
                            //如果两次的时间差＜1s，就不执行操作
                            if(!isMove){
                            Intent intent = new Intent(context, AddNewNoteActivity.class);
                            context.startActivity(intent);
                            }
                        }
                        break;
                }
                return true;
            }
        });
    }

    public void xuanzhuan(float start,float end,String str){
        ObjectAnimator.ofFloat(bt_, "rotation", start, end)//
                .setDuration(100)//
                .start();
        bt_.setText(str+"");
    }
}